/* internal/js/menu/form.js */

function resetFields () {
	document.getElementById('menuID').value = '';
	document.getElementById('menuItemID').value = '';
	document.getElementById('itemInMenuID').value = '';
	document.getElementById('name').value = '';
	document.getElementById('description').value = '';
	document.getElementById('itemInMenuList').innerHTML = '';
	document.getElementById('itemNumber').value = '';
	document.getElementById('itemName').value = '';
	
	hideFieldMessage('nameMessage');
	hideFieldMessage('itemNumberMessage');
	
	selectResultRow(0);
	selectItemInMenuRow(0);
	selectMenuItemRow(0);
}

function loadPage () {
	listMenuItem();
}


/* Build Menu Result Table */

function startResultsTable () {
	var html = '<table class="searchResults">' +
	           '<tr>' +
	           '<th> Name </th>' +
	           '<th> Description </th>' +
	           '</tr>';
	return html;
}


function buildTable (menus) {
	var html = startResultsTable();
	var count = menus.childNodes.length;
	var ids = new Array(count);

	for (var loop = 0; loop < count; loop++) {
		var menu = menus.childNodes[loop];
		var id = menu.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		
		ids[loop] = id;
				
		var name;
		if (menu.getElementsByTagName('name')[0].childNodes.length == 0)
			name = '';
		else
			name = menu.getElementsByTagName('name')[0].childNodes[0].nodeValue;
		
		var description;
		if (menu.getElementsByTagName('description')[0].childNodes.length == 0)
			description = '';
		else
			description = menu.getElementsByTagName('description')[0].childNodes[0].nodeValue;
		
		html += addResultRow(id, name, description);
	
	}
	
	html += endResultsTable(ids);
	return html;
}

function addResultRow (id, name, description) {
	var	html =
		'<form id="listMenuForm' + id + '">' +
		'<input id="name" value="' + name + '" type="hidden" />' +
		'<input id="description" value="' + description + '" type="hidden" />' +
		'</form>' +
		'<tr id="listMenuRow' + id + '" class="searchResultsRow" onclick="showResult(\'' + id + '\'),' +
		'listItemInMenu(\'' + id + '\')">' +
		'<td align="center">' + name + '</td>' +
		'<td align="center">' + description + '</td>';
	return html;
}

function endResultsTable (ids) {
	var html =
		'<input id="resultsIds" value="' + ids.join() +'" type="hidden" />' + 
		'</table>';
	return html;
}



function selectResultRow (id) {
	if (document.getElementById('resultsIds') != null) {
		var ids = document.getElementById('resultsIds').value.split(',');
		var count = ids.length;
		for (var loop = 0; loop < count; loop++) {
			document.getElementById('listMenuRow' + ids[loop]).className = 'searchResultsRow';
		}
		try {
			document.getElementById('listMenuRow' + id).className = 'selectedResultsRow';
		} catch (e) {
		}
	}
}

function showResult (id) {
	selectResultRow(id);
	var frm = document.getElementById('listMenuForm' + id);

	document.getElementById('menuID').value = id;
	document.getElementById('name').value = frm.name.value;
	document.getElementById('description').value = frm.description.value;
}

/* END Build Menu Result Table */

/* Build Menu Item Table */

function buildMenuItemTable(type, menuitems) {
	var html = '<table class="searchResults">' +
    '<tr>' +
    '<th> Item No. </th>' +
    '<th> Name </th>' +
    '<th> Price </th>';
	if (type == 'MenuItem')
		html += '<th> Menu Name </th>';
	html += '</tr>';
	var count = menuitems.childNodes.length;
	var ids = new Array(count);
	var counter = 0;
	for (var loop = 0; loop < count; loop++) {
		var menuitem = menuitems.childNodes[loop];
		var id = menuitem.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		
		// get the show option if it's menu item
		
		if (type == 'MenuItem') {
			var showAll = document.showOpForm.showOp[0].checked;
			var menu = menuitem.getElementsByTagName('menu-name')[0];
			if (menu != null)
				var available = menuitem.getElementsByTagName('menu-name')[0].childNodes.length == 0;
			else 
				available = false;
			if (showAll || available) {
				ids[counter++] = id;
				// Get all the information from menuitem xml
				if (menuitem.getElementsByTagName('menu-item-name')[0].childNodes.length > 0)
					var name = menuitem.getElementsByTagName('menu-item-name')[0].childNodes[0].nodeValue;
				else	
					name = '';
				
				if (menuitem.getElementsByTagName('number')[0].childNodes.length > 0)
					var itemNo = menuitem.getElementsByTagName('number')[0].childNodes[0].nodeValue;
				else
					itemNo = '';
				
				if (menuitem.getElementsByTagName('price')[0].childNodes.length > 0)
					var price = menuitem.getElementsByTagName('price')[0].childNodes[0].nodeValue;
				else
					price = '';
				
				if (menuitem.getElementsByTagName('menu-name')[0].childNodes.length > 0)
					var menuName = menuitem.getElementsByTagName('menu-name')[0].childNodes[0].nodeValue;
				else
					menuName = '';
				
				html += addMenuItemRow(type, id, name, itemNo, price, menuName);
			}
			newIds = new Array(counter);
			for (var i = 0; i < counter; i++)
				newIds[i] = ids[i];
		}
		else {
			ids[loop] = id;
			
			// Get all the information from menuitem xml
			if (menuitem.getElementsByTagName('menu-item-name')[0].childNodes.length > 0)
				name = menuitem.getElementsByTagName('menu-item-name')[0].childNodes[0].nodeValue;
			else
				name = '';
			
			if (menuitem.getElementsByTagName('number')[0].childNodes.length > 0)
				itemNo = menuitem.getElementsByTagName('number')[0].childNodes[0].nodeValue;
			else
				itemNo = '';
			
			if (menuitem.getElementsByTagName('price')[0].childNodes.length > 0)
				price = menuitem.getElementsByTagName('price')[0].childNodes[0].nodeValue;
			else
				price = '';
			
			html += addMenuItemRow(type, id, name, itemNo, price, '');
		}
	}
	if (type == 'MenuItem')
		html +=
			'<input id="' + type +'Ids" value="' + newIds.join() +'" type="hidden" />' + 
			'</table>';
	else
		html +=
			'<input id="' + type +'Ids" value="' + ids.join() +'" type="hidden" />' + 
			'</table>';
	return html;
}

function addMenuItemRow(type, id, name, itemNo, price, menuName) {
	var	html =
		'<form id="list' + type + 'Form' + id + '">' +
		'<input id="name" value="' + name + '" type="hidden" />' +
		'<input id="itemNo" value="' + itemNo + '" type="hidden" />' +
		'<input id="price" value="' + price + '" type="hidden" />' +
		'</form>' +
		'<tr id="list' + type + 'Row' + id + '" class="searchResultsRow" onclick="set' + type + 'Id(\'' + id + '\'), select' + type + 'Row(\'' + id + '\')">' +
		'<td align="right">' + itemNo  + '</td>' +
		'<td align="center">' + name + '</td>' +
		'<td align="right">' + '$'+ parseFloat(price).toFixed(2) + '</td>';
	if (type == 'MenuItem')
		html += '<td align="center">' + menuName + '</td>';
	html += '</tr>';
	return html;
}

function selectMenuItemRow (id) {
	if (document.getElementById('MenuItemIds') != null) {
		var ids = document.getElementById('MenuItemIds').value.split(',');
		var count = ids.length;
		for (var loop = 0; loop < count; loop++) {
			document.getElementById('listMenuItemRow' + ids[loop]).className = 'searchResultsRow';
		}
		try {
			document.getElementById('listMenuItemRow' + id).className = 'selectedResultsRow';
		} catch (e) {
		}
	}
}

function selectItemInMenuRow (id) {
	if (document.getElementById('ItemInMenuIds') != null) {
		var ids = document.getElementById('ItemInMenuIds').value.split(',');
		var count = ids.length;
		for (var loop = 0; loop < count; loop++) {
			document.getElementById('listItemInMenuRow' + ids[loop]).className = 'searchResultsRow';
		}
		try {
			document.getElementById('listItemInMenuRow' + id).className = 'selectedResultsRow';
		} catch (e) {
		}
	}
}


function setMenuItemId(id) {
	document.getElementById('menuItemID').value = id;
}

function setItemInMenuId(id) {
	document.getElementById('itemInMenuID').value = id;
}